package com.zsw.procure_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsw.procure_system.pojo.Userinfo;
import com.zsw.procure_system.pojo.result.ApiResult;
import com.zsw.procure_system.service.UserinfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/login")
    public ApiResult<String> getUserInfo(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        QueryWrapper<Userinfo> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username", username);
        List<Userinfo> userinfoList = userinfoService.list(objectQueryWrapper);
        if (CollectionUtils.isNotEmpty(userinfoList)) {
            Userinfo userinfo = userinfoList.get(0);
            String password1 = userinfo.getPassword();
            if (password1.equals(password)) {
                if (username.equals("admin")) {
                    return ApiResult.ok(username);
                }
                return ApiResult.ok("vip");
            }
        }
        return ApiResult.fail("该用户未注册");

    }

}
